//== SAGITTARIUS ============================================================================

//== BEGINNING OF CODE ===============================================================

#include "sagittarius/core/SagittariusCore.h"
#include "sagittarius/builtins/modules/sbml.h"
#include "sagittarius/io/io.h"
#include <string.h>
#include <malloc.h>

sg_value* sg_mod_io_savesbml(void* ctx, sg_value* _net, sg_value* _path) {
    SG_WRITE(2, "sg_mod_io_savesbml\n");
    // cast
    sg_network_val* net = sg_cast_to_network(_net);
    sg_str_val* path = sg_cast_str(_path);
    // perform
    #if SAGITTARIUS_ENABLE_LIBSBML
        sg_mod_sbml_write(net, path);
    #else
        AN(0, "Sagittarius has not been built with libSBML support");
    #endif
    return NULL;
}

void sg_load_mod_sbml(sg_value* vmod) {
    //sg_mod_io_members members;
    
    SG_WRITE(2, "Loading module sbml\n");
    AT(vmod->t == (sg_type*)sg_global_session()->bt->mod, "Wrong type for module");
    
    symcode s_savesbml = symtab_get_or_insert_cstr(sg_global_session()->st, "savesbml");
    
    sg_fun_val* f_savesbml=sg_create_fun();
    // symbol
    f_savesbml->s = s_savesbml;
    // fptr
    f_savesbml->f = (void*)(sg_mod_io_savesbml);
    // nargs
    f_savesbml->nargs = 2;
    
    // create field for "println"
    sg_value** ppval = bf_do_faccessl(vmod, s_savesbml);
    *ppval = (sg_value*)f_savesbml;
}

void sg_unload_mod_sbml(sg_value* mod) {
    SG_WRITE(2, "Unloading module sbml\n");
}

void sg_register_mod_sbml(sg_session* s) {
    sg_module* m=sg_create_module(s);
    // symbol "sbml"
    m->s = symtab_get_or_insert_cstr(s->st, "sbml");
    
    m->load_mod = sg_load_mod_sbml;
    sg_register_mod(s->ml, m);
}
